import java.util.ArrayList;


public class initial {
	protected boardInfor newBoard = new boardInfor();
	
	protected ArrayList<aListInfor> newList = new ArrayList<aListInfor>();
	protected ArrayList<Integer> BIdOfNewList = new ArrayList<Integer>();
	
	protected ArrayList<cardInfor> newCard = new ArrayList<cardInfor>();
	protected ArrayList<Integer> LIdOfNewCard = new ArrayList<Integer>();
	
	protected ArrayList<attachmentInfor> newAttachment = new ArrayList<attachmentInfor>();
	protected ArrayList<Integer> CIdOfNewAttachment = new ArrayList<Integer>();
	
	protected ArrayList<memberInfor> newMember = new ArrayList<memberInfor>();
	protected ArrayList<Integer> BIdOfNewMember = new ArrayList<Integer>();
	
	protected ArrayList<labelInfor> newLabel = new ArrayList<labelInfor>();
	protected ArrayList<Integer> BIdOfNewLabel = new ArrayList<Integer>();
	
	protected ArrayList<board> boardList = new ArrayList<board>();
	
	protected boolean initialBoard(ArrayList<Integer> BId,ArrayList<Integer> UId,ArrayList<String> BTitle){
		/*
		 * we have	:
		 * 		BId array:integer
		 * 		UId	array:integer
		 * 		BTitle	array:String
		 */
		try{
			if ( BId.size() != UId.size() && UId.size() != BTitle.size() ){	// test if the data sets is correct
				throw new Exception("Initial datas in Board have wrong number of tuples");
			}
			else{
				for (int i = 0; i < BId.size(); i++){	// initial the board
					newBoard.createNewBoard(BTitle.get(i), UId.get(i),BId.get(i));
				}
			}
			
		}
		catch (Exception e){
			System.out.println(e.getMessage());
			return false;
		}
		return true;
	}
	protected boolean initialList(	ArrayList<Integer> LId		,ArrayList<Integer> BId
								,	ArrayList<String> LTitle	,ArrayList<Boolean> visible){
		/*
		 * we have	:
		 * 		LId array:Integer
		 * 		BId array:Integer
		 * 		LTitle array:String
		 * 		visible array:Boolean
		 */
		try{
			if (	   LId.size() 	  != BId.size() && BId.size() != LTitle.size() 
					&& LTitle.size()  != visible.size()	) {
				
				throw new Exception("Initial datas in list have wrong number of tuples");
			}
			else {
				/*
				 * here will add "list" into "voard" according BId
				 */
				for (int count = 0; count < LId.size(); count++) {
					boolean find = false; // to check if a list already have "list" on it
					
					// find the "list stream" on a board, the add a new "list" to the "list stream"
					for(int index = 0; index < BIdOfNewList.size(); index++) {
			            if (BIdOfNewList.get(index) == BId.get(count)){
			            	this.newList.get(index).createNewList(LTitle.get(count), LId.get(count));
			            	if (visible.get(count) == true)
			            		this.newList.get(index).editArchiving(LId.get(count));
			            	else
			            		this.newList.get(index).editUnarchiving(LId.get(count));
			            	find = true;
			            	break;
			            }
			        }
					
					// if can not find the "card stream" on a list, then means the list haven't attach any card
					if ( !find ){
						aListInfor createList = new aListInfor();
						createList.createNewList(LTitle.get(count), LId.get(count));
						if (visible.get(count) == true)
							createList.editArchiving(LId.get(count));
						else
							createList.editUnarchiving(LId.get(count));
						
						newBoard.attachLists(createList, BId.get(count));
						this.newList.add(createList);
						this.BIdOfNewList.add(BId.get(count));
					}
				}
				
			}
			
		}
		catch(Exception e){
			System.out.println(e.getMessage());
			return false;
		}
		return true;
	}
	protected boolean initialCard(	ArrayList<Integer> CId,		ArrayList<Integer> UId,
									ArrayList<Integer> LId,		ArrayList<String> dateStart,
									ArrayList<String> dateEnd,	ArrayList<String> CTitle,
									ArrayList<String> content,	ArrayList<Integer> LabelId){
		/*
		 * we have	:
		 * 		CID 		array:Integer
		 * 		LId 		array:Integer
		 * 		UId 		array:Integer
		 * 		DataStart	array:String
		 * 		dataEnd		array:String
		 * 		CTitle 		array:String
		 * 		content 	array:String
		 */
		
		try{
			if ( CId.size() != UId.size() && UId.size() != LId.size() && LId.size() != dateStart.size() &&
				 dateStart.size() != dateEnd.size() && dateEnd.size() != CTitle.size() && CTitle.size() != content.size() 
				 && LabelId.size() != content.size() ){
				
				throw new Exception("Initial datas in card have wrong number of tuples");
			}
			else {
				for (int count = 0; count < LId.size(); count++) {
					boolean find = false; // to check if a board already have "list" on it
					
					// find the "list stream" on a board, the add a new "list" to the "list stream"
					for(int index = 0; index < LIdOfNewCard.size(); index++) {
			            if (LIdOfNewCard.get(index) == LId.get(count)){
			            	this.newCard.get(index).createNewCard(CTitle.get(count), CId.get(count), UId.get(count));
			            	
			            	label toAdd = getLabelById(LabelId.get(count));
			            	this.newCard.get(index).attachLabel(toAdd, CId.get(count));
			            	find = true;
			            	break;
			            }
			        }
					
					// if can not find the "list stream" on a board, then means the board haven't attach any list
					if ( !find ){
						cardInfor createCard = new cardInfor();
						createCard.createNewCard(CTitle.get(count), CId.get(count), UId.get(count));
						label toAdd = getLabelById(LabelId.get(count));
		            	createCard.attachLabel(toAdd, CId.get(count));
		            	
						for (aListInfor doc : newList){
							for (aList doc2 : doc.getAll()){
								if (doc2.getId() == LId.get(count)){
									doc2.attachCardList(createCard);
									this.newCard.add(createCard);
									this.LIdOfNewCard.add(LId.get(count));
								}
							}
						}
						
					}
				}
				
			}
		}
		catch (Exception e){
			System.out.println(e.getMessage());
		}
		
		return true;
	}
	protected boolean initialAttachment(){
		
		return true;
	}
	protected boolean initialLabel(ArrayList<Integer> LabelId, ArrayList<String> LabelName, ArrayList<Integer> BId){
		try{
			if ( LabelId.size() != LabelName.size() && LabelName.size() != BId.size() ) {
				
				throw new Exception("Initial datas in list have wrong number of tuples");
			}
			else {
				for (int count = 0; count < LabelId.size(); count++) {
					boolean find = false;
					
					for(int index = 0; index < BIdOfNewLabel.size(); index++) {
			            if (BIdOfNewLabel.get(index) == BId.get(count)){
			            	this.newLabel.get(index).addNewLabel(LabelId.get(count), LabelName.get(count));
			            	find = true;
			            	break;
			            }
			        }
					
					// if can not find the "card stream" on a list, then means the list haven't attach any card
					if ( !find ){
						labelInfor createLabel = new labelInfor();
						createLabel.addNewLabel(LabelId.get(count), LabelName.get(count));
						newBoard.attachLabels(createLabel, BId.get(count));
						this.newLabel.add(createLabel);
						this.BIdOfNewLabel.add(BId.get(count));
					}
				}
				
			}
			
		}
		catch(Exception e){
			System.out.println(e.getMessage());
			return false;
		}
		return true;
	}
	protected boolean initialMember(ArrayList<Integer> UId, ArrayList<Boolean> isAdmin, ArrayList<Integer> BId){
		
		/*
		 * we have	:
		 * 		UId 		array:Integer
		 * 		isAdmin		array:boolean
		 * 		BId			array:Integer
		 */
		
		try{
			if ( UId.size() != isAdmin.size() && isAdmin.size() != BId.size() ){
				
				throw new Exception("Initial datas in member have wrong number of tuples");
			}
			else {
				for (int count = 0; count < UId.size(); count++) {
					boolean find = false; // to check if a board already have "list" on it
					
					// find the "list stream" on a board, the add a new "list" to the "list stream"
					for(int index = 0; index < BIdOfNewMember.size(); index++) {
			            if (BIdOfNewMember.get(index) == BId.get(count)){
			            	this.newMember.get(index).addNewMember(UId.get(count), isAdmin.get(count));
			            	find = true;
			            	break;
			            }
			        }
					
					// if can not find the "list stream" on a board, then means the board haven't attach any list
					if ( !find ){
						memberInfor createMember = new memberInfor();
						createMember.addNewMember(UId.get(count), isAdmin.get(count));
						
						newBoard.attachMembers(createMember, BId.get(count));
						this.newMember.add(createMember);
						this.BIdOfNewMember.add(BId.get(count));
						
					}
				}
				
			}
		}
		catch (Exception e){
			System.out.println(e.getMessage());
		}
		
		return true;
	}
	protected label getLabelById(int Id){
		try {
			for (labelInfor doc : newLabel){
				for ( label doc2 : doc.getAll() ){
					if (doc2.getId() == Id){
						return doc2;
					}
				}
			}
		}
		catch(Exception e){
			System.out.println("Null result of label with id : "+Id);
		}
		return null;
	}
}
